#include <stdio.h>
#include <stdlib.h>
#include "BinaryTree.h"
#include <time.h>
#include <MyString.h>
#include "Set.h"

int IsBigger(void *ptr1, void *ptr2)
{
    long n1 = (long)ptr1;
    long n2 = (long)ptr2;
    return n1 - n2;
}

void PrintInt(void *ptr)
{
    printf("%ld ",(long)ptr);
}

int main()
{
    srand(time(NULL));
    BSTree *tree = InitBSTree();

    for(long i = 0; i < 10; i++)
    {
        long a = rand() % 2;
        printf("%ld ",a);
        InsertElement(tree,(void*)a,IsBigger);
    }

    printf("\n遍历:\n");

    TravelMid(tree,PrintInt);
    printf("\n");


    printf("输入你要删除的数:");
    long num;
    scanf("%ld",&num);
    FindByElement(tree,(void*)num, IsBigger);
    BTNode *node = FindByElement(tree,(void*)num, IsBigger);
    if(node == NULL)
    {
        printf("没找到！\n");
    }
    else
    {
        printf("找到节点的值是%ld\n",(long)GetNodeData(node));
    }

    RemoveByElement(tree,(void*)num, IsBigger);
    printf("\n遍历:\n");
    TravelMid(tree, PrintInt);
    printf("\n");
    printf("树中节点个数是:%d\n",GetNodeNumber(tree)); 
    return 0;
}

struct Student
{
    int id;
    MyStr name;
    int age;
};

typedef struct Student Stu;

Stu * CreateStu(const int id,const char *name,const int age)
{
    Stu *s = (Stu*)malloc(sizeof(Stu));
    if(s == NULL)
    {
        return NULL;
    }
    s->id = id;
    s->age = age;
    s->name = GetString(name);
    return s;
}

void ClearStu(void *ptr)
{
    Stu *s = (Stu*)ptr;
    ClearString(&s->name);
    free(s);
}

void PrintStu(void *ptr)
{
    if(ptr == NULL)
    return;
    Stu *s = (Stu*)ptr;
    printf("学生学号:%d 学生姓名:%s 学生年龄:%d\n",s->id,s->name.string,s->age);
}

int SortByAge(void *ptr1, void *ptr2)
{
    Stu *s1 = (Stu*)ptr1;
    Stu *s2 = (Stu*)ptr2;
    return s1->age - s2->age;
}
int SortById(void *ptr1, void *ptr2)
{
    Stu *s1 = (Stu*)ptr1;
    Stu *s2 = (Stu*)ptr2;
    return s1->id - s2->id;
}
int SortByName(void *ptr1,void *ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    Stu *s2 = (Stu *)ptr2;
    return (strcmp(s1->name.string,s2->name.string));
}

int FindByAge(void *ptr1, ElementType element)
{
    Stu *s1 = (Stu*)ptr1;
    long age = (long)element;
    return s1->age - age;
}
int FindById(void *ptr1, ElementType element)
{
    Stu *s1 = (Stu*)ptr1;
    long id = (long)element;
    return s1->id - id;
}
int FindByName(void *ptr1,void *ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    char *s = (char *)ptr2;
    return strcmp(s1->name.string,s);
}



// int main()
// {
//     set *s = InitSet(SortByAge,PrintStu,FindByAge);

//     InsertSet(s,CreateStu(1,"zhangsan",21));
//     InsertSet(s,CreateStu(2,"lisi",20));
//     InsertSet(s,CreateStu(3,"wangwu",22));

//     TravelSet(s);
//     printf("\n找到的学生为:\n");
//     PrintStu((Stu*)FindSet(s,(void *)22));
//     printf("\n删除后学生为:\n");
//     RemoveSetByElement(s,(void*)22);
//     TravelSet(s);
//     return 0;
// }